#!/usr/bin/perl

require "./mailadmin-lib.pl";
&ReadParse();

my ($k, $i, $j, @vols, @baktime, $mailroot, $alertmsg);
my (@pathline, $mntroot);

@vols = &get_volumes();

$mntroot = &get_mnt_root();
$mailroot = &qmail_dir_get();

$path = $in{'dirpath'};
unless ($path)
{
    $path = $vols[0];
}

if ($path)
{
    @pathline = split(/\/+/, $path);
}

if (@vols < 1)
{
    $alertmsg= $text{'msg_no_uservol'};
}

&header($text{'edit_loadout_title'}, "", "mail_edit", 0, 1);

print "<form name=qdform action=maildir_load.cgi method=post onsubmit='return do_submit();'>\n";

print "<table class=mode1 border=0>\n";
print "<tr><td>$text{'loadout_dir'}</td>";
print "<td><select name=diraddr class=list4 size=1 onchange=\"return chg_diraddr();\">\n";
for ($i = 0 ; $i < @vols; $i++)
{
    printf "<option value=\"$vols[$i]\" %s>$text{'path_in_vol'}&nbsp;$vols[$i]\n", ($path eq $vols[$i]) ? "selected" : "";
    if ($pathline[0] eq $vols[$i])
    {
	$tb_dash = "";
	$values = $vols[$i];
	for ($j = 1; $j < @pathline; $j++)
	{
	    $tb_dash = $tb_dash . "&nbsp;&nbsp;";
	    $values = $values . '/' . $pathline[$j];
	    printf "<option value=\"%s\" %s>$text{'path_in_vol'}&nbsp;$values\n", $values, ($values eq $path) ? "selected" : "";
	}
    }
}
print "</select>";
print "</td></tr>\n";

@childir = &get_child_dir($mntroot . $path);

print "<tr><td valign=top>$text{'childir'}</td><td>";
print "<select name=childir class=list4 size=10 onclick=\"return clk_childir();\">\n";
for ($i = 0; $i < @childir ; $i++)
{
    print "<option value=\"$childir[$i]\">$childir[$i]\n";
}
print "</select>";
print "</td></tr>\n";

print "<tr><td>$text{'bak_fname'}</td><td>$config{'mailbakfname'}</td></tr>\n";
print "<tr><td>$text{'bak_req_udata'}</td><td><input type=radio name=req_udata value='userdata' checked>$text{'yes'}\n";
print "<input type=radio name=req_udata value='nouserdata'>$text{'no'}";
print "</td></tr>";

print "</table>";

print "<p><input name=dirpath type=hidden class=text value=\"$in{'dirpath'}\">"; 
print "<p><input name=selvol type=hidden class=text value=\"$in{'selvol'}\">";

print "<p><input type=submit class=button1 name=btn_loadout value=\"$text{'btn_loadout'}\" onclick=\"return btnloadout();\">";

print "<p>$text{'discrp_loadout_detail'}";

print "</form>\n";


print <<EOF;
<script>
function do_submit()
{
//    document.forms[0].btn_backup.disabled = true;
//    document.forms[0].btn_load.disabled = true;
    document.forms[0].btn_loadout.disabled = true;

}

function btnloadout()
{
    document.qdform.action="save_loadout.cgi";
}

function clk_childir()
{
    var dir1, dir2;
    var idx;

    if (document.qdform.diraddr.length == 1)
    {
	dir1 = document.qdform.diraddr.value;
    }

    if (document.qdform.diraddr.length > 1)
    {
	idx = document.qdform.diraddr.selectedIndex;
	if (idx >= 0)
	{
	    dir1 = document.qdform.diraddr.options[idx].value;
	}
    }

    if (document.qdform.childir.length == 1)
    {
	dir2 = document.qdform.childir.value;
    }

    if (document.qdform.childir.length > 1)
    {
	idx = document.qdform.childir.selectedIndex;
	if (idx >= 0)
	{
	    dir2 = document.qdform.childir.options[idx].value;
	}
    }
    
    if ((document.qdform.childir.length > 0) && (document.qdform.childir.selectedIndex >= 0))
    {
	
	document.qdform.dirpath.value = (dir1 + "/" + dir2);
	document.qdform.action = "edit_loadout.cgi";
	document.qdform.submit();
    }
    
}

function chg_diraddr()
{
    var dir1;
    var idx;

    if (document.qdform.diraddr.length == 1)
    {
	dir1 = document.qdform.diraddr.value;
    }

    if (document.qdform.diraddr.length > 1)
    {
	idx = document.qdform.diraddr.selectedIndex;
	if (idx >= 0)
	{
	    dir1 = document.qdform.diraddr.options[idx].value;
	}
    }

    if (document.qdform.diraddr.length > 0)
    {
	document.qdform.dirpath.value = dir1;
	document.qdform.action = "edit_loadout.cgi";
	document.qdform.submit();
    }
}

</script>
EOF

&footer("/", $text{"index"}, "maildir_edit.cgi", $text{"maildir_title"});
